package com.letoken.platform.repo.app.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.admin.req.user.QueryAppAssetsListReq;
import com.letoken.platform.repo.admin.res.user.QueryAppAssetsListRes;
import com.letoken.platform.repo.app.po.AppUserWallet;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * Le账户-用户资产表 Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2022-12-10
 */
public interface AppUserWalletMapper extends BaseMapper<AppUserWallet> {
    /**
     * 修改用户金额 减传负数
     * @param availableBalance 可用金额
     * @param freezeBalance 冻结金额
     * @param releasedBalance 等待释放金额
     * @param version 版本号
     * @param userId 用户id
     * @param tokenId 币种id
     * @return
     */
    int updateUserAmountByUserIDandCoin(@Param("availableBalance")BigDecimal availableBalance,@Param("freezeBalance")BigDecimal freezeBalance,@Param("releasedBalance")BigDecimal releasedBalance,@Param("version")int version,@Param("userId")Integer userId,@Param("tokenId") Integer tokenId);

    Page<QueryAppAssetsListRes> queryUserAssetsList(@Param("page") Page<QueryAppAssetsListRes> page, @Param("req") QueryAppAssetsListReq req);



    @Select("select IFNULL(available_balance,0) from app_user_wallet where user_id = #{userId} and token_id = #{tokenId} ")
    BigDecimal getAvailableBalance(@Param("tokenId") Integer tokenId,@Param("userId") Integer userId);


    @Select("select id from app_user_wallet where user_id = #{userId} and token_id = #{tokenId} ")
    Integer checkoutTokenIdAndUserId(@Param("tokenId") Integer tokenId,@Param("userId") Integer userId);



    @Select("select * from app_user_wallet where id = #{id} for update")
    AppUserWallet selectForUpdate(@Param("id") Integer id);

    @Select("select * from app_user_wallet where id = #{id}")
    AppUserWallet selectById(@Param("id") Integer id);

    @Select("select * from app_user_wallet where user_id = #{userId} and token_id = #{tokenId} for update")
    AppUserWallet selectForUpdate2(@Param("userId") Integer userId, @Param("tokenId") Integer tokenId);

//    @Select("select * from app_user_wallet where user_id = #{userId} and token_id = #{tokenId}")
//    AppUserWallet selectOne(@Param("userId") Integer userId, @Param("tokenId") Integer tokenId);

    @Update("update app_user_wallet set  available_balance = available_balance + #{amount} where id = #{id} limit 1")
    Integer updateBalance(@Param("id") Integer id,@Param("amount") BigDecimal amount);
    BigDecimal queryReleasedBalance(@Param("userId") Integer userId);



    @Select("select  DISTINCT user_id from app_user_wallet where id < #{id}")
    List<Integer> walletUserIds(@Param("id") Integer id);


    @Select("SELECT MAX(id)  from app_user_wallet")
    Integer walletMaxId();

    @Select("select user_id userId from app_user_wallet w  where w.token_id=2 and w.available_balance>=100;")
    List<Integer> getUserIdList();

    @Select("select * from app_user_wallet w  where w.available_balance>0 and w.token_id in (1,2);")
    List<AppUserWallet> getWalletList();

    @Select("select user_id userId, (available_balance+freeze_balance+released_balance) availableBalance  from app_user_wallet where token_id=5 and  (available_balance+freeze_balance+released_balance)>=1")
    List<AppUserWallet> getLPAmount();
}
